//
//  HMTabBarController.swift
//  swift-新浪微博0
//
//  Created by 赵玉忠 on 16/8/29.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 直接赋值不可以，因为tabBar是一个只读的属性。可以使用 KVC 去间接设置值
        // self.tabBar = HMTabBar()
        let tabBar = HMTabBar()
        
        // 定义一个闭包
        let closure = { [weak self] in
            print("撰写按钮点击，请弹出菜单 \(self)")
            
            let v = HMComposeView()
            
            // 对照第三方框架去设计api
            v.show(target: self!)

//            let window = UIApplication.shared.keyWindow
//            window?.addSubview(v)

        }
        // 将该闭包传给tabBar
        tabBar.composeButtonClosure = closure;
        self.setValue(tabBar, forKeyPath: "tabBar")
        
        addChildViewControllers()
        
        // 设置显示颜色为
        // tabBar.tintColor = UIColor.orange
    }
    
    func addChildViewControllers()  {
        addChildViewController(vc: HMHomeTableViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(vc: HMMessageTableViewController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(vc: HMDiscoverTableViewController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(vc: HMProfileTableViewController(), title: "我", imgName: "tabbar_profile")
    }
    
    func addChildViewController(vc:UIViewController,title:String,imgName:String){
        
        vc.title = title;
        // 设置默认的图标
        vc.tabBarItem.image = UIImage(named: imgName)
        // 设置选中的图标
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")
        // 设置文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: UIControlState.selected)
        
        // 通过传入的控制器初始化一个导航控制器出来
        let navVC = HMNavigationController(rootViewController: vc)
        // 将导航控制器添加到tabBarVc里面
        addChildViewController(navVC)
    }

}
